import { NodeCanvasRenderingContext2D } from 'canvas';
import { Tween } from './tweens/Tween';
import { Pair } from './types/Pair';

export abstract class Actor {
	public render(ctx: NodeCanvasRenderingContext2D, frame: number, time: number) {
		let properties = Object.create(this);
		this.tweens.forEach(pair => {
			const value = Reflect.get(properties, pair.getLeft());
			const interpolation = pair.getRight().interpolate(value, frame);
			Reflect.set(properties, pair.getLeft(), interpolation);
		});
		this._paint(properties, ctx, frame, time);
	}

	public addTween(propertyName: string, tween: Tween) {
		this.tweens.push(new Pair<string, Tween>(propertyName, tween));
	}

	protected abstract _paint(properties: Actor & Record<string, number>, ctx: NodeCanvasRenderingContext2D, frame: number, time: number): void;

	private tweens: Pair<string, Tween>[] = [];
}
